package com.nageoffer.jobtrain.projects.toolkit;

import com.nageoffer.jobtrain.common.web.exception.ClientException;

/**
 * 字符串拼接工具类
 * author：唐晨
 */
public class StringSplicingUtils {
    /**
     * 用户提交整体代码拼接
     *
     * @param userSubmitCode 用户实际提交的代码
     * @param mainFunction   main函数代码
     * @return 拼接整体代码
     */
    public static String codeConnect(String userSubmitCode, String mainFunction) {
        String targetCharacter = "}";
        int targetLastIndex = userSubmitCode.lastIndexOf(targetCharacter);
        if (targetLastIndex != -1) {
            return userSubmitCode.substring(0, targetLastIndex) + "\n" + mainFunction + "\n" + userSubmitCode.substring(targetLastIndex);
        }
        throw new ClientException("代码拼接失败");
    }
}

